const WebSocket = require('ws');
const rp  = require('request-promise')
const schedule = require('node-schedule');
const Sequelize = require('sequelize')

const key = '637DDB1B-C7D1-4355-BFD9-4A623C0F142D';
var redis = require("redis"),
    client = redis.createClient({
      host: '47.75.114.151',
      port: '6379'
    });
function request(url) {
  return rp({
    url: `https://rest.coinapi.io${url}`,
    json: true,
    headers: {
     'X-CoinAPI-Key': key
    },
    strictSSL: true
  });
}
request('/v1/exchangerate/USD/USDT')
.then(result=>{
})
module.exports = async function(app) {
  const sequelize = app.get('sequelizeClient')
  const quoteArray = await sequelize.query('SELECT asset_id FROM payroll.asset  where isShow = 1;',{ type: Sequelize.QueryTypes.SELECT})
  const coinArray = quoteArray.map(({asset_id}) => {
    return asset_id
  })
  let len = coinArray.length
  let coinsPrice = {

  }
  while(len > Object.keys(coinsPrice).length) {
    coinArray.forEach(item => {
      request(`/v1/exchangerate/${item}/USD`)
      .then(({rate})=>{
        coinsPrice[item] = rate
      })
      .catch(e=>{
        debugger
      })
    })
  }
  debugger
  var ws = new WebSocket('ws://39.104.93.80:10002/');
  ws.on('open', function open() {
    var hello = {
      "type": "subscribe",
      "symbols": ["MDCUSD"]
    };
    ws.send(JSON.stringify(hello));
  });
  ws.on('message', function incoming(data) {
    debugger
  })
}